package com.chixing.service.impl;

import com.chixing.mapper.ItineraryPlanMapper;
import com.chixing.mapper.TravelSightsMapper;
import com.chixing.pojo.*;
import com.chixing.service.FreeToWalkService;
import com.chixing.util.ServerResponse;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class FreeToWalkServiceImpl implements FreeToWalkService {

    @Autowired
    private TravelSightsMapper travelSightsMapper;
    @Autowired
    private ItineraryPlanMapper itineraryPlanMapper;
    @Override
    public ServerResponse getSightsByMultiCondition(Float minPrice,Float maxPrice, Integer level, String type,Integer pageNum) {

        TravelSightsExample travelSightsExample = new TravelSightsExample();
        TravelSightsExample.Criteria criteria = travelSightsExample.createCriteria();
        if(minPrice!=1){
            criteria.andTravelTicketsPriceGreaterThanOrEqualTo(minPrice);
        }
        if(maxPrice!=1){
            criteria.andTravelTicketsPriceLessThanOrEqualTo(maxPrice);
        }
        if(level!=0){
            criteria.andTravelSightsLevelEqualTo(level);
        }
        if(!type.equals("1")){
            criteria.andTravelSightsTypeLike("%"+type+"%");
        }
        PageHelper.startPage(pageNum,3);
        List<TravelSights> travelSights = travelSightsMapper.selectByExample(travelSightsExample);
        PageInfo<TravelSights> pageInfo = new PageInfo<>(travelSights);
        return ServerResponse.getSuccess(pageInfo);
    }

    @Override
    public ServerResponse getPlanByMultiCondition(Float minPrice, Float maxPrice, Integer perNum, Integer days, Integer pageNum) {
        ItineraryPlanExample itineraryPlanExample = new ItineraryPlanExample();
        ItineraryPlanExample.Criteria criteria = itineraryPlanExample.createCriteria();
        if (minPrice!=-1){
            criteria.andItineraryBudgetGreaterThanOrEqualTo(minPrice);
        }
        if(maxPrice!=-1){
            criteria.andItineraryBudgetLessThanOrEqualTo(maxPrice);
        }
        if(perNum!=-1){
            criteria.andNumberAdultsEqualTo(perNum);
        }
        if(days!=-1){
            criteria.andItineraryTravelDaysEqualTo(days);
        }

        Page page = PageHelper.startPage(pageNum,3);
        ArrayList<ItineraryPlanSightsVO> listVo = new ArrayList<>();
        List<ItineraryPlan> itineraryPlans = itineraryPlanMapper.selectByExample(itineraryPlanExample);
        for (ItineraryPlan itineraryPlan:itineraryPlans
             ) {
            ItineraryPlanSightsVO itineraryPlanSightsVO = new ItineraryPlanSightsVO();
            itineraryPlanSightsVO.setItineraryPlan(itineraryPlan);
            TravelSights travelSights = travelSightsMapper.selectByPrimaryKey(itineraryPlan.getTravelSightsId());
            itineraryPlanSightsVO.setTravelSights(travelSights);
            listVo.add(itineraryPlanSightsVO);
        }
        PageInfo pageInfo = page.toPageInfo();
        pageInfo.setList(listVo);
        return ServerResponse.getSuccess(pageInfo);
    }

    @Override
    public ServerResponse getSightById(Integer id) {
        TravelSights travelSights = travelSightsMapper.selectByPrimaryKey(id);
        if (travelSights==null){
            return ServerResponse.getFail(null);
        }else {
            return ServerResponse.getSuccess(travelSights);
        }
    }


}
